package com.quanyue.music.system.service.impl;

import com.quanyue.auto.util.AssertUtils;
import com.quanyue.music.domain.constant.RechargeRuleId;
import com.quanyue.music.domain.entity.RechargeRule;
import com.quanyue.music.system.mapper.RechargeRuleMapper;
import com.quanyue.music.system.service.RechargeRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class RechargeRuleServiceImpl implements RechargeRuleService {
    @Autowired
    private RechargeRuleMapper rechargeRuleMapper;

    @Override
    public void addRule(RechargeRule rechargeRule) {
        rechargeRule.setCreateTime(LocalDateTime.now());
        rechargeRule.setModifyTime(LocalDateTime.now());
        rechargeRuleMapper.insert(rechargeRule);
    }

    @Override
    public void updateRule(RechargeRule rechargeRule) {
        rechargeRule.setModifyTime(LocalDateTime.now());
        rechargeRuleMapper.update(rechargeRule);
    }

    @Override
    public void deleteRule(String id) {
        if (id.equals(RechargeRuleId.DEFAULTRULE_ONE) || id.equals(RechargeRuleId.DEFAULTRULE_TWO))
            throw new AssertUtils.ValidateException("一月、二月只能修改");
        rechargeRuleMapper.deleteOne(id);
    }

    @Override
    public List<RechargeRule> findRules() {
        return rechargeRuleMapper.findAllOrderBy();
    }
}
